package com.globalresources.ui.struts.action.menu;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.globalresources.business.inface.IMenuService;
import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.po.Menu;
import com.globalresources.po.User;
import com.globalresources.po.Groups;
import com.globalresources.po.Authority;
import com.globalresources.business.inface.IGroupsService;
import com.globalresources.business.inface.IAuthorityService;

/**
 * 
 * @since 创建时间：Jul 22, 2009 12:50:59 AM
 */
public class MenuAciton extends AbstractAction implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private IUserService userService;
	private IMenuService menuService;
	private List<Menu> menus;
	private Menu menu;
	private String message;
	private HttpServletRequest request;	
	private List<Groups> groups;
	private List<Authority> listauthority;
	private IGroupsService groupsService;
	private IAuthorityService authorityService;

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String list() {
		menus = new ArrayList<Menu>();
		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		if (null != obj && obj instanceof UserDetails) {
			UserDetails userDetails = (UserDetails) obj;
			GrantedAuthority[] auths = userDetails.getAuthorities();
			for (int i = 0; i < auths.length; i++) {
				GrantedAuthority auth = auths[i];
				menus.addAll(menuService.getMenus(auth.getAuthority()));
			}
		}
		return SUCCESS;
	}

	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @param null
	 * @return
	 */
	public String saveOrUpdateMenu() {
		menuService.saveOrUpdateMenus(menu);
		return SUCCESS;
	}

	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @return
	 */
	public String listMenu() {
		menus = menuService.listMenus();
		return SUCCESS;
	}

	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @return
	 */
	public String listMenuGroups() {
		menus = menuService.listGroup();
		return SUCCESS;
	}
    
	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @return
	 */
	public String listMenuByGroups() {
		String commonId=request.getParameter("commonId");
		menus = menuService.listByGroup(commonId);
		return SUCCESS;
	}
	
	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @return
	 */
	public String listMenuByAuthority() {
		String commonId=request.getParameter("commonId");
		menus = menuService.listByAuthor(commonId);
		return SUCCESS;
	}
	
	/***
	 * @author songduanlei@gmail.com
	 * list Menu
	 * @return
	 */
	public String listMenuAuthority() {
		menus = menuService.listAuthor();
		return SUCCESS;
	}

	/***
	 * @author songduanlei@gmail.com
	 * Edit Menu Info
	 * @return
	 */
	public String editMenu() {
		String id = request.getParameter("id");
		menu = (Menu) menuService.getMenuById(Integer.parseInt(id));
		groups=groupsService.listGroups();
		listauthority=authorityService.listAuthority();	
		return SUCCESS;
	}

	/***
	 * @author songduanlei@gmail.com
	 * delete Menu Info
	 * @return
	 */
	public String deleteMenu() {
		String id = request.getParameter("id");
		if (menuService.deleteMenuById(Integer.parseInt(id))) {			
			setMessage("delete Success, Please continue your operate!Thanks");
			return SUCCESS;
		} else {
			setMessage("delete again, or contact the system administrator! Thanks!");
			return ERROR;

		}
	}

	/***
	 * @author songduanlei@gmail.com
	 * Save Menu Info
	 * @return
	 */
	public String addMenu() {
		groups=groupsService.listGroups();
		listauthority=authorityService.listAuthority();	
		return SUCCESS;
	}
	
	
	/***
	 * @author songduanlei@gmail.com
	 * Save Menu Info
	 * @return
	 */
	public String saveMenu() {
		menuService.saveMenus(menu);
		return SUCCESS;
	}

	/***
	 * @author songduanlei@gmail.com
	 * SaveOrUpdate Menu Info
	 * @return
	 */
	public String SaveOrUpdateMenu() {
		menuService.saveOrUpdateMenus(menu);
		return SUCCESS;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}

	public List<Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * @param menu the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the groups
	 */
	public List<Groups> getGroups() {
		return groups;
	}

	/**
	 * @param groups the groups to set
	 */
	public void setGroups(List<Groups> groups) {
		this.groups = groups;
	}


	/**
	 * @return the groupsService
	 */
	public IGroupsService getGroupsService() {
		return groupsService;
	}

	/**
	 * @param groupsService the groupsService to set
	 */
	public void setGroupsService(IGroupsService groupsService) {
		this.groupsService = groupsService;
	}

	/**
	 * @return the authorityService
	 */
	public IAuthorityService getAuthorityService() {
		return authorityService;
	}

	/**
	 * @param authorityService the authorityService to set
	 */
	public void setAuthorityService(IAuthorityService authorityService) {
		this.authorityService = authorityService;
	}

	/**
	 * @return the listauthority
	 */
	public List<Authority> getListauthority() {
		return listauthority;
	}

	/**
	 * @param listauthority the listauthority to set
	 */
	public void setListauthority(List<Authority> listauthority) {
		this.listauthority = listauthority;
	}



}
